<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="isLoading">
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮
            el-icon-plus 加号小图标-->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addRoleFn"
            >新增角色</el-button>
            <!-- 表格  el-table-column渲染的-->
            <el-table :data="roleList">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- :current-page=""表示当前是那一页
             :page-sizes=""动态设置页容量的下拉列表
             :page-size当前的页容量
             layout表示分页的展示样式
             total表示一共还有多少条
             @current-change表示的点击页码的时候执行的事件
             @size-change="handleSizeChange" 动态设置页容量的时候执行的事件-->
            <div style="margin-top: 10px; display: flex; justify-content: flex-end">
              <el-pagination
                :current-page="page"
                :page-sizes="[2,3,4,5]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
              />
            </div>

          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.remarks" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="showTitle"
        :visible="showDialog"
        :close-on-click-modal="false"
        @close="closeDialog"
      >
        <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="confirmAddRole">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleListApi, deleteRoleApi, addRoleApi, getRoleDetailApi, editRpleApi, getCompanyInfoApi } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      page: 1,
      pagesize: 2,
      roleList: [],
      total: 0,
      isLoading: false,
      showDialog: false,
      // 对应的弹窗内容
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  // 计算属性
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    // 调用方法
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    async getRoleList() {
      this.isLoading = true
      // 获取第一页的前十条数据
      const { data } = await getRoleListApi(this.page, this.pagesize)
      this.roleList = data.rows
      this.total = data.total
      this.isLoading = false
    },
    // 公司信息
    async getCompanyInfo() {
      const { data } = await getCompanyInfoApi(this.companyId)
      this.companyForm = data
    },
    // 点击页码
    handleCurrentChange(val) {
      this.page = val
      this.getRoleList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getRoleList()
    },
    // 新增角色
    confirmAddRole() {
      this.$refs.roleForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          await editRpleApi(this.form)
          this.$message.success('修改成功')
        } else {
          await addRoleApi(this.form)
          this.$message.success('添加成功')
        }
        // this.page = Math.ceil((this.total + 1) / this.pagesize)
        this.getRoleList()
        this.closeDialog()
      })
    },
    addRoleFn() {
      this.showDialog = true
    },
    closeDialog() {
      // 控制弹窗隐藏
      this.showDialog = false
      // 清空的是 绑定了prop的表单域的状态和数据
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },

    // 编辑功能
    async editRole(id) {
      // 数据回显
      const { data } = await getRoleDetailApi(id)
      this.form = data
      // 控制弹窗显示
      this.showDialog = true
    },

    // 删除操作
    delRole(id) {
      // console.log('123')
      this.$confirm('你确定要删除嘛', '温馨提示').then(async() => {
        await deleteRoleApi(id) // 调用删除的接口
        if (this.roleList.length === 1 && this.page > 1) {
          this.page--
        }
        this.getRoleList() // 加载页面
        this.$message.success('删除角色成功')
      }).catch(error => {
        console.log(error)
      })
    }

  }
}
</script>

<style>

</style>
